package dao;

import javafx.scene.image.Image;
import model.Users;
import util.ConnMySQL;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
/**
 * Created by Hasee on 2018/11/23
 */
public class UsersDao {
    /**
     * 删除数据库中的数据
     * @param id
     */
    public void delete(int id)throws Exception{
        Connection conn=ConnMySQL.getConnection();
        PreparedStatement ps=null;
        String sql="delete from news where idusers =?";
        try{
            ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            int a=ps.executeUpdate();
            if(a>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(ps!=null){
                    ps.close();
                }if(conn!=null){
                    conn.close();
                }
            }catch(Exception e2){
                e2.printStackTrace();
            }
        }
    }

    /**
     * 换头像
     * @param users
     * @return
     */

    public int setIcon(Users users){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        FileInputStream fis = null;
        try {
            connection=ConnMySQL.getConnection();
            String sql ="update users set icon=? where idusers=?";
            preparedStatement = connection.prepareStatement(sql);
            InputStream str=new FileInputStream(users.getIcon());//把图片放进数据库中
            long length=(long)str.available();//来获取文件的大小
            preparedStatement.setBinaryStream(1, str, length);//public final void setBinaryStream(int parameterIndex,java.io.InputStream x,long length)
            preparedStatement.setString(2,users.getIdusers());
            resultSet = preparedStatement.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1;
    }
    /**
     * 登录
     * @param user
     * @return
     */
    public String userLogin(Users user){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try{
            connection = ConnMySQL.getConnection();
            String sql = "select idusers from users where name=? and password=?";
            //PreparedStatement使用？占位符，setXxx设置其占位符的值
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getName());
            preparedStatement.setString(2,user.getPassword());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return resultSet.getString("idusers");
            }
            return "false";
        }catch (Exception e){
            e.printStackTrace();
            return "false";
        }finally {
            if (resultSet != null){
                try {
                    resultSet.close();
                    resultSet =null;
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 注册 添加用户
     * @param user
     * @return
     */
    public String addUsers(Users user){
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet affectedSet = null;

        String idusers = null;
        try {
            conn = ConnMySQL.getConnection();
            String sql = "insert into users(name, password,email) values(?, ?, ?)";
//			[!]注意这里的insert获取id的写法
            preStmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            preStmt.setString(1, user.getName());
            preStmt.setString(2, user.getPassword());
            preStmt.setString(3,user.getEmail());
            preStmt.executeUpdate();
            affectedSet = preStmt.getGeneratedKeys();
            if (affectedSet.next()) {
                idusers = affectedSet.getString(1);
                System.out.println(idusers);
            }
            //user.setIdusers(idusers);
            return idusers;
        } catch  (SQLDataException ex) {
            if (ex.getErrorCode()==1062){
                System.out.println("用户已经存在");
                return "exist";
            }
            ex.printStackTrace();
            return null;
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }finally {
            if (affectedSet != null) {
                try {
                    affectedSet.close();
                    affectedSet = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (preStmt != null) {
                try {
                    preStmt.close();
                    preStmt = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}

